package com.zafu.dao;

import com.zafu.entity.AccountInfo;
import com.zafu.util.ElmDbConn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class AccountBean {
    private Connection conn;

    public AccountBean() {
    }


    /**添加账号**/
    public int Add(AccountInfo info){
        int count = -1;
        conn =  ElmDbConn.GetConnection();

        if (conn == null) {
            System.out.println("数据库连接失败");
            return -1;
        }

        try {
            StringBuffer sBuffer = new StringBuffer();
            sBuffer.append("insert into Account")
                    .append("(telephone_number, password, name, people_name, head_image, introduction)")
                    .append("values ( ?, ?, ?, ?, ?, ?) ");
            PreparedStatement st = conn.prepareStatement( sBuffer.toString() );
            int i = 0;
            st.setString(++i, info.getPhone());
            st.setString(++i, info.getPassword());
            st.setString(++i, info.getName());
            st.setString(++i, info.getPeople_name());
            st.setString(++i, info.getHead_image());
            st.setString(++i, info.getIntroduction());

            count = st.executeUpdate();
            System.out.printf("插入成功\n", count);
            return 0;
        }
        catch (SQLException e){
            System.out.println("插入失败："+ e.getMessage());
            e.printStackTrace();
            return -1;
        }
        finally {
            if (conn != null) {
                try{
                    conn.close();
                }
                catch (SQLException e) {
                    System.out.println("关闭连接失败\n" + e.getMessage());
                }
            }
        }
    }

    /**获得所有账号**/
    public ArrayList<AccountInfo> GetAll() {
        conn = ElmDbConn.GetConnection();
        System.out.println(conn);

        ArrayList<AccountInfo> list = new ArrayList<>();
        try {
            StringBuffer sBuffer = new StringBuffer();
            sBuffer.append( " select * ")
                    .append(" from Account");
            PreparedStatement st = conn.prepareStatement( sBuffer.toString());
            ResultSet rs = st.executeQuery();
            while(rs.next()) {
                AccountInfo info = GetDataFromResultSet(rs);
                list.add(info);
            }
        }
        catch (SQLException e) {
            System.out.printf("数据库查询失败\n" + e.getMessage());
            e.printStackTrace();
        }
        finally {
            if (conn != null) {
                try{
                    conn.close();
                }
                catch ( SQLException e ) {
                    System.out.printf("关闭连接失败\n" + e.getMessage());
                }
            }
        }
        return list;
    }

    private AccountInfo GetDataFromResultSet(ResultSet rs ){
        AccountInfo info = null;
        try{
            String phone = rs.getString("telephone_number");
            String password = rs.getString( "password" );
            String name = rs.getString( "name" );
            String people_name = rs.getString( "people_name" );
            String head_image = rs.getString("head_image");
            String introduction = rs.getString("introduction");

            info = new AccountInfo(phone, password, name, people_name, head_image, introduction);
        }catch( SQLException e ){
            System.out.printf( "数据库查询失败\n" + e.getMessage()  );
            e.printStackTrace();
        }
        return info;
    }

    /**修改简介*/
    public int ChangeIntroduction(String phone, String new_introduction){
        conn = ElmDbConn.GetConnection();
        System.out.println(conn);

        try {
            StringBuffer sBuffer = new StringBuffer();
            sBuffer.append( " update Account ")
                    .append(" set introduction = ?")
                    .append(" where telephone_number = ?");
            PreparedStatement st = conn.prepareStatement( sBuffer.toString());
            st.setString(1, phone);
            st.setString(2, new_introduction);
            st.executeQuery();
        }
        catch (SQLException e) {
            System.out.printf("数据库查询失败\n" + e.getMessage());
            e.printStackTrace();
        }
        finally {
            if (conn != null) {
                try{
                    conn.close();
                }
                catch ( SQLException e ) {
                    System.out.printf("关闭连接失败\n" + e.getMessage());
                }
            }
        }
        return 0;
    }
    /**修改用户名*/
    public int ChangeName(String phone, String new_name){
        conn = ElmDbConn.GetConnection();
        System.out.println(conn);

        try {
            StringBuffer sBuffer = new StringBuffer();
            sBuffer.append( " update Account ")
                    .append(" set name = ?")
                    .append(" where telephone_number = ?");
            PreparedStatement st = conn.prepareStatement( sBuffer.toString());
            st.setString(1, phone);
            st.setString(2, new_name);
            st.executeQuery();
        }
        catch (SQLException e) {
            System.out.printf("数据库查询失败\n" + e.getMessage());
            e.printStackTrace();
        }
        finally {
            if (conn != null) {
                try{
                    conn.close();
                }
                catch ( SQLException e ) {
                    System.out.printf("关闭连接失败\n" + e.getMessage());
                }
            }
        }
        return 0;
    }

    /**修改用户名*/
    public int ChangePassword(String phone, String new_password){
        conn = ElmDbConn.GetConnection();
        System.out.println(conn);

        try {
            StringBuffer sBuffer = new StringBuffer();
            sBuffer.append( " update Account ")
                    .append(" set password = ?")
                    .append(" where telephone_number = ?");
            PreparedStatement st = conn.prepareStatement( sBuffer.toString());
            st.setString(1, phone);
            st.setString(2, new_password);
            st.executeQuery();
        }
        catch (SQLException e) {
            System.out.printf("数据库查询失败\n" + e.getMessage());
            e.printStackTrace();
        }
        finally {
            if (conn != null) {
                try{
                    conn.close();
                }
                catch ( SQLException e ) {
                    System.out.printf("关闭连接失败\n" + e.getMessage());
                }
            }
        }
        return 0;
    }

    /**修改头像*/
    public int ChangeHead(String phone, String new_head) {
        conn = ElmDbConn.GetConnection();
        System.out.println(conn);

        try {
            StringBuffer sBuffer = new StringBuffer();
            sBuffer.append(" update Account ")
                    .append(" set head_image = ?")
                    .append(" where telephone_number = ?");
            PreparedStatement st = conn.prepareStatement(sBuffer.toString());
            st.setString(1, phone);
            st.setString(2, new_head);
            st.executeQuery();
        } catch (SQLException e) {
            System.out.printf("数据库查询失败\n" + e.getMessage());
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    System.out.printf("关闭连接失败\n" + e.getMessage());
                }
            }
        }
        return 0;
    }
}
